<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Observers;

use App\Models\Article;
use Illuminate\Support\Facades\Cache;

class ArticleObserver
{
    /**
     * 文章保存或更新事件.
     */
    public function saved()
    {
        Cache::forget('article');
        Cache::forget('today_recommend');

        $todayRecommend = Article::query()
            ->where('id','!=', 7)
            ->where('status', true)
            ->inRandomOrder()
            ->first();

        $article = Article::query()
            ->where('id','!=', 7)
            ->where('status', true)
            ->orderByDesc('click')
            ->limit(10)
            ->get();

        Cache::forever('article', $article);
        Cache::forever('today_recommend', $todayRecommend);
    }

    public function deleted()
    {
        Cache::forget('article');
        Cache::forget('today_recommend');

        $todayRecommend = Article::query()
            ->where('id','!=', 7)
            ->where('status', true)
            ->inRandomOrder()
            ->first();

        $article = Article::query()
            ->where('id','!=', 7)
            ->where('status', true)
            ->orderByDesc('click')
            ->limit(10)
            ->get();

        Cache::forever('article', $article);
        Cache::forever('today_recommend', $todayRecommend);
    }
}
